//
//  VOHMoreViewController.m
//  VoiceOfHeart
//
//  Created by 吉龙 on 16/8/22.
//  Copyright © 2016年 吉龙. All rights reserved.
//

#import "VOHMoreViewController.h"
#import "VOHMoreView.h"
#import "VOHMusicTableViewCell.h"
#import "VOHMusicDetailModel.h"
#import "NetHandler.h"
#import "VOHMusicNavigationBar.h"
#import "VOHMusicDetailViewController.h"
@interface VOHMoreViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)VOHMoreView *moreView;
@property(nonatomic, assign)BOOL isHot;
@property(nonatomic, assign)BOOL isMostPlay;
@property(nonatomic, assign)BOOL isRecent;
@property(nonatomic, strong)NSMutableArray *hotArray;
@property(nonatomic, strong)NSMutableArray *MostPlayArr;
@property(nonatomic, strong)NSMutableArray *recentArr;
@end

@implementation VOHMoreViewController
-(NSMutableArray *)hotArray{
    if (!_hotArray) {
        _hotArray = [NSMutableArray array];
    }
    return _hotArray;
}

-(NSMutableArray *)MostPlayArr{
    if (!_MostPlayArr) {
        _MostPlayArr = [NSMutableArray array];
    }
    return _MostPlayArr;
}

-(NSMutableArray *)recentArr{
    if (!_recentArr) {
        _recentArr = [NSMutableArray array];
    }
    return _recentArr;
}
-(void)loadView{
    self.moreView = [[VOHMoreView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.moreView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    VOHMusicNavigationBar *bar = (VOHMusicNavigationBar *)self.navigationController.navigationBar;
    __weak typeof(self)weakSelf = self;
    bar.backBtn.block = ^(VOHButton *sender){
        [weakSelf.navigationController popViewControllerAnimated:YES];
        
    };
    
    self.navigationItem.hidesBackButton = YES;
    [self.moreView.segment addTarget:self action:@selector(clickSegment:) forControlEvents:UIControlEventValueChanged];
    self.moreView.moreTableView .delegate = self;
    self.moreView.moreTableView.dataSource = self;
    [self.moreView.moreTableView registerClass:[VOHMusicTableViewCell class] forCellReuseIdentifier:@"moreCell"];
    self.isHot = YES;
    self.isRecent = NO;
    self.isMostPlay = NO;
    [self handleHot];
}

-(void)clickSegment:(UISegmentedControl *)sender{
    if (sender.selectedSegmentIndex == 0) {
        self.isHot = YES;
        self.isRecent = NO;
        self.isMostPlay = NO;
        [self handleHot];
    }else if (sender.selectedSegmentIndex == 1){
        self.isHot = NO;
        self.isRecent = YES;
        self.isMostPlay = NO;
        [self recentHandel];
    }else{
        self.isHot = NO;
        self.isRecent = NO;
        self.isMostPlay = YES;
        [self mostPlayHandel];
    }
}

#pragma mark------解析数据
static int i = 1;
static int j = 1;
static int k = 1;
-(void)handleHot{
    [NetHandler getDataWithUrl:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/album?calcDimension=hot&categoryId=2&device=iPhone&pageId=%d&pageSize=20&position=1&status=0&tagName=%@&title=精选歌单", i, self.tagName] completion:^(NSData *data) {
        if (data != nil) {

            NSDictionary *rootDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            NSArray *array = [rootDic objectForKey:@"list"];
            for (NSDictionary *dict in array) {
                VOHMusicDetailModel *model = [[VOHMusicDetailModel alloc] init];
                [model setValuesForKeysWithDictionary:dict];
                [self.hotArray addObject:model];
            }
            
            [self.moreView.moreTableView reloadData];
        }
    }];

}


-(void)recentHandel{
    [NetHandler getDataWithUrl:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/album?calcDimension=recent&categoryId=2&device=iPhone&pageId=%d&pageSize=20&position=1&status=0&tagName=%@&title=精选歌单", j, self.tagName] completion:^(NSData *data) {
        if (data != nil) {
            NSDictionary *rootDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            NSArray *array = [rootDic objectForKey:@"list"];
            for (NSDictionary *dict in array) {
                VOHMusicDetailModel *model = [[VOHMusicDetailModel alloc] init];
                [model setValuesForKeysWithDictionary:dict];
                [self.recentArr addObject:model];
            }
            
            [self.moreView.moreTableView reloadData];
        }
    }];

}

-(void)mostPlayHandel{
    [NetHandler getDataWithUrl:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/album?calcDimension=mostplay&categoryId=2&device=iPhone&pageId=%d&pageSize=20&position=1&status=0&tagName=%@&title=精选歌单", k, self.tagName] completion:^(NSData *data) {
        if (data != nil) {
            NSDictionary *rootDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            NSArray *array = [rootDic objectForKey:@"list"];
            for (NSDictionary *dict in array) {
                VOHMusicDetailModel *model = [[VOHMusicDetailModel alloc] init];
                [model setValuesForKeysWithDictionary:dict];
                [self.MostPlayArr addObject:model];
            }
            
            [self.moreView.moreTableView reloadData];
        }
    }];

}

#pragma mark----tableView的代理

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.isHot) {
        return self.hotArray.count;
    }
    if (self.isRecent) {
        return self.recentArr.count;
    }
    if (self.isMostPlay) {
        return self.MostPlayArr.count;
    }
    return 0;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    VOHMusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"moreCell" forIndexPath:indexPath];
    if (self.isHot) {
        cell.model = self.hotArray[indexPath.row];
    }
    if (self.isRecent) {
        cell.model = self.recentArr[indexPath.row];
    }
    if (self.isMostPlay) {
        cell.model = self.MostPlayArr[indexPath.row];
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    VOHMusicDetailViewController *detailCtr = [[VOHMusicDetailViewController alloc] init];
    [self.navigationController pushViewController:detailCtr animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
